﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SST_BL;
using System.Data;

namespace TCU
{
    public partial class ManejoRoles : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CargarRoles();
            }
        }

        private void CargarRoles() {
            DataTable dt = MainBL.ListarRoles();
            if (dt == null)
            {
                dt = new DataTable();
                dt.Columns.Add("ID", typeof(int));
                dt.Columns.Add("Nombre", typeof(string));
                dt.Rows.Add(0, "No hay ningun informatico...");
                Roles.AutoGenerateDeleteButton = false;
                Roles.AutoGenerateEditButton = false;
            }
            else
            {
                Roles.AutoGenerateDeleteButton = true;
                Roles.AutoGenerateEditButton = true;
            }
            Roles.DataSource = dt;
            Roles.DataBind();            
        }

        protected void Roles_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("AddRow"))
            {
                TextBox txtNombreRol = Roles.FooterRow.FindControl("txtNombre") as TextBox;
                MainBL.AgregarRol(txtNombreRol.Text);
                this.CargarRoles();
            }
        }

        protected void Roles_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Label lbID = Roles.Rows[e.RowIndex].FindControl("lbID") as Label;
            TextBox txtNombre = Roles.Rows[e.RowIndex].FindControl("txtEditNombre") as TextBox;
            MainBL.EditarRol(int.Parse(lbID.Text), txtNombre.Text);
            Roles.EditIndex = -1;
            this.CargarRoles();
        }

        protected void Roles_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Roles.EditIndex = e.NewEditIndex;
            CargarRoles();
        }//protected void Roles_RowEditing(object sender, GridViewEditEventArgs e)

        protected void Roles_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            Roles.EditIndex = -1;
            this.CargarRoles();
        }

        protected void Roles_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Label id = Roles.Rows[e.RowIndex].FindControl("lbID") as Label;

            MainBL.EliminarRol(int.Parse(id.Text));
            this.CargarRoles();
        }//protected void Roles_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

    }
}